<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2019/5/22
 * Time: 9:04
 */
namespace app\admin\controller;
use app\admin\model\AdminUser;
use app\admin\model\Functions;
use app\admin\model\InsTalls;
use app\admin\model\MyBackups;
use app\admin\model\Subject;
use app\admin\model\UpFile;
use mwechat\src\Curl;
use think\Request;
use app\admin\model\Enclosure as Enclosures;

class Api
{
    //安装模块
    public function install(Request $request){
        $modular = $request->param('modular',null);
        if(!$modular){
            return json_my_response('安装失败');
        }
        $all = config('modular.');
        $thismodular = '';
        foreach ($all as $key=>$value){
            if($value['dir']==$modular){
                $thismodular = $value;
                $value['install'] = true;
            }
            $all[$key] = $value;

        }
        $obj = new MyBackups();
        $tianjia = false;
        if(!$thismodular['sql']){
            $tianjia = true;
            $thismodular['sql'] = './Data/default.sql';
            $obj->huanyuan($thismodular['sql'],[
                '`admin_user`'=>config('database.prefix').$modular.'_'.'admin_user',
                '`column`'=>config('database.prefix').$modular.'_'.'column',
                '`databases`'=>config('database.prefix').$modular.'_'.'databases',
                '`database_system`'=>config('database.prefix').$modular.'_'.'database_system',
                '`enclosure`'=>config('database.prefix').$modular.'_'.'enclosure',
                '`fields`'=>config('database.prefix').$modular.'_'.'fields',
                '`jurs`'=>config('database.prefix').$modular.'_'.'jurs',
                '`keywords`'=>config('database.prefix').$modular.'_'.'keywords',
                '`models`'=>config('database.prefix').$modular.'_'.'models',
                '`roles`'=>config('database.prefix').$modular.'_'.'roles',
                '`systems`'=>config('database.prefix').$modular.'_'.'systems',
                '`users`'=>config('database.prefix').$modular.'_'.'users',
                '`qiniu`'=>config('database.prefix').$modular.'_'.'qiniu',
                '`wechats`'=>config('database.prefix').$modular.'_'.'wechats',
                '`mokuais`'=>config('database.prefix').$modular.'_'.'mokuais',
            ]);
        }else{
           Subject::huanyuan(['sql'=>$thismodular['sql']]);
        }
        $dir = Functions::getDirList('../application');
        if(!in_array($modular,$dir)){
            chmod('../application',0777);
            mkdir('../application/'.$modular,0777,true);
        }
        if(Functions::changeConfig('../config/Modular.php',$all)){
            if($tianjia){
                InsTalls::insert($modular);
            }
            return json_my_response('安装成功',200,url('admin/index/index?modular='.$modular));
        }else{
            return json_my_response('安装失败');
        }
    }

    /**后端登录
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function signin(Request $request){
        $data = $request->param();
        $val = my_yanzheng($data,[
            'code|验证码'=>'require|captcha',
            'user|用户名'=>'require',
            'pwd|密码'=>'require'
        ]);
        if(!$val['code']){
            return json_my_response($val['msg']);
        }
        $user = AdminUser::where('user',$data['user'])->findOrEmpty()->toArray();
        if(!$user){
            return json_my_response('账号不存在');
        }
        if(my_decryption($data['pwd'],$user['pwd'],true)){
            if($bool = $this->souquan($request)){
                session('adminuser',$user);
                return json_my_response('登录成功',200);
            }else{
                return json_my_response('网站未授权');
            }
        }else{
            return json_my_response('密码或账号错误');
        }
    }
    /**添加多张图片
     * @param Request $request
     * @return array
     */
    public function addfiles(Request $request){
//        $file = $request->file();
        $upfile = new UpFile();
        $data = $upfile->setFile($request)->Preservation();
        return json($data);
    }

    /**添加文件
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function deleteFile(Request $request){
        $id = $request->param('id');
        if(UpFile::delFile($id)){
            return json_my_response('删除成功',200);
        }else{
            return json_my_response('删除失败');
        }
    }

    /**分片上传
     * @return \think\response\Json
     */
    public function fenpianupfile(){
        $bool = UpFile::Fragmentation('./fenpian/file_material_tmp','./uploads/'.session('modular'));
        if(isset($bool['success'])&&$bool['success']){
            $data['yuan_name'] = $bool['oldName'];
            $array = explode('/',$bool['filePath']);
            $data['new_name'] = end($array);
            $data['path'] = ltrim($bool['filePath'],'.');
            $data['quan_path'] = $bool['filePath'];
            $data['create_time'] = time();
            $upfile = new UpFile();
            $data['id'] = $upfile->addEnclosure($data);
            $data['path'] = rtrim(config('webroot'),'/').$bool['filePath'];
            return json($data);
        }
        return json($bool);
    }

    /**获取七牛云token
     * @return bool|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getqiniutoken(){
        $upfiel = new UpFile();
        return $upfiel->getQiniuToken();
    }
    public function addqiniu(Request $request){
        $name = $request->param('newname');
        $yuanname = $request->param('yuanname');
        $data['new_name'] = $name;
        $data['yuan_name'] = $yuanname;
        $upfile = new UpFile();
        $qiniu = $upfile->getQiNiu();
        $data['path'] = $qiniu['url'].'/'.$name;
        $data['quan_path'] = $qiniu['url'].'/'.$name;
        $data['status'] = 1;
        $data['id'] = $upfile->addEnclosure($data);
        return json($data);
    }
    public function ches(){
        if(strstr('http://www.baidu.com','http://')){
            echo 1;
        }else{
            echo 2;
        }
    }
    public function bianji(Request $request){
        $upfile = new UpFile();
        $bool = $upfile->setFile($request)->Preservation();
        $array['state'] = 'SUCCESS';
        $array['url'] = $bool['path'];
        $array['title'] = $bool['new_name'];
        $array['original'] = $bool['yuan_name'];
        $as = explode('.',$bool['new_name']);
        $array['type'] = $as[1];
        $array['size'] = 1000*1000;
        return json($array);
    }
    public function souquan(Request $requery){
        $data = $requery->param();
        $newdata = [];
        if(isset($data['user'])&&$data['user']){
            $newdata['zhanghao'] = $data['user'];
        }
        if(isset($data['pwd'])&&$data['pwd']){
            $newdata['pasworder'] = $data['pwd'];
        }
        if(session('modular')){
            $newdata['mokuai'] = session('modular');
        }
        $newdata['yuming'] = $requery->domain();
        $curl = new Curl();
        $curl = $curl->fasong('http://www.maowenke.cn/index/index/jianche',1,1,$newdata)->jsonToArray()->get();
        if($curl['code']==200){
            return true;
        }else{
            return false;
        }
    }
}
